package pdf;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jichao
 * @date 2022/11/22 17:02
 */
public class PdfAddPicture {
    public static String PATH = "D:\\\\A.pdf";
    public static String PNG_PATH = "C:\\Users\\PC-a\\Desktop\\nft.png";
    public static String OUT_PATH = "D:\\\\aaaa.pdf";

/*    public static void main(String[] args) throws Exception {
        Map<String, Object> paramMap = new HashMap<>();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        paramMap.put("date_time", dateTimeFormatter.format(LocalDateTime.now()));
        paramMap.put("date", dateTimeFormatter.format(LocalDateTime.now()).substring(0, 10));
        *//* String outPath = "D:\\A.pdf";*//*
        String templateDirectory = "src/main/resources/templates";
        String templateContent = GetHtmlContent.getTemplateContent(templateDirectory, "generationpdf.html", paramMap);
        ByteArrayOutputStream byteArrayOutputStream = GeneratePDF.html2Pdf(templateContent);
        System.err.println(byteArrayOutputStream);
        new PdfAddPicture().modifyPdf(byteArrayOutputStream);
    }*/



    public void modifyPdf(ByteArrayOutputStream byteArrayOutputStream) throws IOException {
/*        //        1、获得PDF文件流对象
        FileInputStream pdfInputStream = new FileInputStream();
//        2、得到PDF文档对象
        PDDocument pdDocument = PDDocument.load(pdfInputStream);
//        3、由Document得到Page对象
        PDPage page = pdDocument.getPage(0);
//        4、通过图片路径和PDF文档对象得到图片image对象
        PDImageXObject image = PDImageXObject.createFromFile(PNG_PATH, pdDocument);
//        5、创建pageStream对象
        PDPageContentStream pageStream = new PDPageContentStream(pdDocument, page, PDPageContentStream.AppendMode.APPEND, false, false);

//   6、pageStream对象绘制图片位置及大小，已PDF文件右下角为原点（x,y）是图片左下角左边，width、height是图片的长和宽
        pageStream.drawImage(image, 100, 700, 50, 50);
        pageStream.close();
//    7、保存PDF到指定路劲
        pdDocument.save(OUT_PATH);
//      8、关闭流
        pdDocument.close();
        pdfInputStream.close();*/

    }

    public static void main(String[] args) throws UnknownHostException {
        String localHost = InetAddress.getLocalHost().getHostAddress();
        String pictureUrl = "http://" + localHost + "/file/downLoad/" ;
        System.out.println(pictureUrl);
    }
}
